//
//  MyGroupActiveViewController.m
//  Findif
//
//  Created by Mac on 2019/3/25.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "MyGroupActiveViewController.h"
#import "GroupActiveTableViewCell.h"
#import "TopicHeaderView.h"
#import "GroupActiveModel.h"
#import "GroupActiveDetailViewController.h"
#import "GroupInfoViewController.h"

@interface MyGroupActiveViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, weak)UIImageView * spaceImageView;
@property (nonatomic, weak)UILabel * spaceLabel;
@property (nonatomic, strong)NSArray <GroupActiveModel * > * myHistoryList;
@property (nonatomic, strong)NSArray <GroupActiveModel * > * myTodayList;

@end

@implementation MyGroupActiveViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"我的活动";
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MyActiveToPersonal:) name:@"MyActiveToGroupInfo" object:nil];
    [self setupUI];
    [self loadData];
}


- (void)loadData{

    [[ZRNetworkTool sharedTool] getMyGroupActivityGoodsWithGroup_id:@"" WithBlock:^(BOOL success, id responseData) {
        
        //        NSLog(@"res===%@",responseData);
        
        if (success) {
            
            NSDictionary * dict = responseData[@"data"];
            //            NSLog(@"dict===%@",dict);
            self.myTodayList = [NSArray yy_modelArrayWithClass:[GroupActiveModel class] json:dict[@"today"]];
            self.myHistoryList = [NSArray yy_modelArrayWithClass:[GroupActiveModel class] json:dict[@"history"]];
        }
        
    }];
    
    [self.tableView.mj_header endRefreshing];
}

- (void)setMyTodayList:(NSArray<GroupActiveModel *> *)myTodayList{
    
    _myTodayList = myTodayList;
    
    [self.tableView reloadData];
}


- (void)setMyHistoryList:(NSArray<GroupActiveModel *> *)myHistoryList{
    
    _myHistoryList = myHistoryList;
    
    [self.tableView reloadData];
}

- (void)setupUI{
    
    self.view.backgroundColor = ColorWithRGB(0xF4F5F6);
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topHeight, kScreenW, kScreenH - topHeight) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ColorWithRGB(0xF4F5F6);
    tableView.rowHeight = 110 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[GroupActiveTableViewCell class] forCellReuseIdentifier:@"MyGroupActiveTableViewCell"];
    [self.tableView registerClass:[TopicHeaderView class] forHeaderFooterViewReuseIdentifier:@"MyGroupActiveHeaderView"];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self loadData];
    }];
    
    [self.view addSubview:self.tableView];
    
    
    UIImageView * spaceImageView = [UIImageView new];
    
    spaceImageView.image = [UIImage imageNamed:@"状态为空"];
    spaceImageView.hidden = YES;
    
    self.spaceImageView = spaceImageView;
    
    [self.tableView addSubview:self.spaceImageView];
    
    
    UILabel * spaceLabel = [UILabel new];
    
    spaceLabel.text = @"你暂无参见任何活动";
    spaceLabel.textColor = [UIColor lightGrayColor];
    spaceLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    spaceLabel.textAlignment = NSTextAlignmentCenter;
    spaceLabel.hidden = YES;
    
    self.spaceLabel = spaceLabel;
    
    [self.tableView addSubview:self.spaceLabel];
}


- (void)viewDidLayoutSubviews{
    
    [self.spaceImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_equalTo(self.tableView);
        make.width.mas_equalTo(169 * SCREENH6);
        make.height.mas_equalTo(136 * SCREENH6);
    }];
    
    [self.spaceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.spaceImageView);
        make.top.mas_equalTo(self.spaceImageView.mas_bottom).offset(10 * SCREENH6);
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (self.myTodayList.count > 0 && self.myHistoryList.count > 0) {
        
        self.spaceLabel.hidden = YES;
        self.spaceImageView.hidden = YES;
        
        return 2;
    }else if (self.myTodayList.count > 0 || self.myHistoryList.count > 0){
        
        self.spaceLabel.hidden = YES;
        self.spaceImageView.hidden = YES;
        
        return 1;
    }else if (self.myTodayList.count == 0 && self.myHistoryList.count == 0){
        
        self.spaceLabel.hidden = NO;
        self.spaceImageView.hidden = NO;
        
        return 0;
    }else{
        
        self.spaceLabel.hidden = YES;
        self.spaceImageView.hidden = YES;
        
        return 0;
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        if (self.myTodayList.count > 0) {
            
            return self.myTodayList.count;
        }else{
            
            return self.myHistoryList.count;
        }
    }else{
        
        return self.myHistoryList.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        GroupActiveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyGroupActiveTableViewCell" forIndexPath:indexPath];
        if (self.myTodayList.count > 0) {
            
            cell.model = self.myTodayList[indexPath.row];
        }else{
            
            cell.model = self.myHistoryList[indexPath.row];
        }
        
        cell.groupLabel.hidden = NO;
        
        return cell;
    }else{
        
        GroupActiveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyGroupActiveTableViewCell" forIndexPath:indexPath];
        
        cell.model = self.myHistoryList[indexPath.row];
        cell.groupLabel.hidden = NO;
        
        return cell;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 30 * SCREENH6;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        
        TopicHeaderView * header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MyGroupActiveHeaderView"];
        
        if (self.myTodayList.count > 0) {
            
            header.titleLabel.text = @"进行中的活动";
        }else if(self.myHistoryList.count > 0){
            
            header.titleLabel.text = @"历史活动";
        }
        
        return header;
    }else{
        
        if (self.myHistoryList.count > 0) {
            
            TopicHeaderView * header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MyGroupActiveHeaderView"];
            
            header.titleLabel.text = @"历史活动";
            
            return header;
        }else{
            
            return nil;
        }
    }
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
    
    if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
        ((UITableViewHeaderFooterView *)view).backgroundView.backgroundColor = ColorWithRGB(0xF4F5F6);
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HiddenBar;
    GroupActiveTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    GroupActiveDetailViewController * detailVC = [GroupActiveDetailViewController new];
    
    detailVC.model = cell.model;
    detailVC.ID = cell.model.id;
    
    [self.navigationController pushViewController:detailVC animated:YES];
}


- (void)MyActiveToPersonal:(NSNotification *)noti{
    
    NSString * ID = [NSString stringWithFormat:@"%@",noti.userInfo[@"group_id"]];
    
    HiddenBar;
    GroupInfoViewController * infoVC = [GroupInfoViewController new];
    
    infoVC.group_id = ID;
    
    [self.navigationController pushViewController:infoVC animated:YES];
}

@end
